/**
 * 角色表格
 */
import React, {Component} from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';
import {Link} from 'dva/router';
import {Table, Icon, Popconfirm, Button} from 'antd';
import getPagination from '../../components/pagination';
import {onSetItemDecorator} from '../../components/export';

const styles = {
  confirmB: {
    marginRight: '1em'
  },
  defaultV: {
    color: '#ccc'
  },
  colorR: {
    color: 'red'
  },
  colorG: {
    color: 'green'
  }
};

const columns = {
  '93c1539d58d34e9c9868cfde8c6c23df': [
    'index',
    'imei',
    'signalStrength',
    'signalSNR',
    'linkQuality',
    'batteryLevel',
    'manufacturer',
    'normal',
    // 'signalECL',
    'sensorFault',
    'fault',
    'pvc',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'result',
    // "status_time",
    'opt',
  ],

  //VH75规格NB  V0.1.0
  '7522fab06c4841c883eca4012834e791': [
    'index',
    'imei',
    // "status_time",
    'signalStrength',
    'signalSNR',
    'linkQuality',
    'batteryLevel',
    'manufacturer',
    'normal',
    'sensorFault',
    'fault',
    'pvc',
    // 'signalECL',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'result',
    'opt',
  ],



  //WSH6100N V2.1.1
  'da3cb6515ebd4236bd69c6f5148753df': [
    'index',
    'imei',
    'signalStrength',
      'iccid',
    'signalSNR',
    'linkQuality',
    'batteryLevel',
    'manufacturer',
    'normal',
    'sensorFault',
    'fault',
    'pvc',
    'signalECL',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    "statusTime",
    'result',
    'opt',
  ],

  //VH75规格NB V0.3.0
  '3abb1ae62b9b494da1ceafeb51165469': [
    'index',
    'imei',
    // "status_time",
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'undervoltageFaultModule',
    'result',
    'opt',
  ],

  //VT3431WN V0.1.0
  '6a1ae2b0f5b84c4ea6807951561c725a': [
    'index',
    'imei',
    // "status_time",
    'undervoltageFault',
    'fireAlarm',
    'rssi',
    'analogQuantity',
    'result',
    'opt',
  ],

  //WSH6100N V1.1.0
  'fa1c1dc4df9b4a3ca0dac26308ac3836': [
    'index',
    'imei',
    // "status_time",
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'undervoltageFaultModule',
    'wirelessmoduleVersion',
    'modulesoftwareVersion',
    'deviceVersion',
    'wirelessmoduleElectricity',
    'componetType',
    'deviceStatus',
    'devicesEvent',
    'packNumber',
    'ccid',
    'controlOlder',
    'result',
    'opt',
  ],

  //VT3431WN V0.2.0
  'a9ad4f923632415a8c6a7ac65b4f61bc': [
    'index',
    'imei',
    // "status_time",
    'undervoltageFault',
    'fireAlarm',
    'rssi',
    'analogQuantity',
    'remote_muffler',
    'remote_selftest',
    'result',
    'opt',
  ],
};

const columnsItems = [
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
    width:'35px',
    fixed: 'left',
  },
  {
    title: 'IMEI号',
    dataIndex: 'imei',
    key: 'imei',
    width:'115px',
    fixed: 'left',
  },
  {
    title:"信号覆盖等级",
    dataIndex:"signalECL",
    key:"signalECL",
  },
  {
    title:"iccid",
    dataIndex:"iccid",
    key:"iccid",
  },

  {
    title:"时间",
    dataIndex:"statusTime",
    key:"statusTime",
  },
  {
    title: '电量',
    dataIndex: 'batteryLevel',
    key: 'batteryLevel',
    // render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '信号强度',
    dataIndex: 'signalStrength',
    key: 'signalStrength',
    // render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '信噪比',
    dataIndex: 'signalSNR',
    key: 'signalSNR',
    // render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '连接质量',
    dataIndex: 'linkQuality',
    key: 'linkQuality',
    // render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  }, {
    title: '厂商代码',
    dataIndex: 'manufacturer',
    key: 'manufacturer',
    // render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '正常',
    dataIndex: 'normal',
    key: 'normal',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '传感器故障',
    dataIndex: 'sensorFault',
    key: 'sensorFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },{
    title: '故障',
    dataIndex: 'fault',
    key: 'fault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },{
    title: '电量不足',
    dataIndex: 'pvc',
    key: 'pvc',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '欠压故障',
    dataIndex: 'undervoltageFault',
    key: 'undervoltageFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '污染故障',
    dataIndex: 'pollutionFault',
    key: 'pollutionFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '静态点故障',
    dataIndex: 'staticPointFault',
    key: 'staticPointFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '丢失故障',
    dataIndex: 'detectorLossFault',
    key: 'detectorLossFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '火警',
    dataIndex: 'fireAlarm',
    key: 'fireAlarm',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '自检',
    dataIndex: 'selfChecking',
    key: 'selfChecking',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '模组欠压故障',
    dataIndex: 'undervoltageFaultModule',
    key: 'undervoltageFaultModule',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  }, {
    title: '模拟量',
    dataIndex: 'analogQuantity',
    key: 'analogQuantity',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '温度',
    dataIndex: 'temperature',
    key: 'temperature',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: 'RSSI',
    dataIndex: 'rssi',
    key: 'rssi',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '无线模组版本号',
    dataIndex: 'wirelessmoduleVersion',
    key: 'wirelessmoduleVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '模组固件版本号',
    dataIndex: 'modulesoftwareVersion',
    key: 'modulesoftwareVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备版本号',
    dataIndex: 'deviceVersion',
    key: 'deviceVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '无线模组电量',
    dataIndex: 'wirelessmoduleElectricity',
    key: 'wirelessmoduleElectricity',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '部件类型',
    dataIndex: 'componetType',
    key: 'componetType',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备状态',
    dataIndex: 'deviceStatus',
    key: 'deviceStatus',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备事件',
    dataIndex: 'devicesEvent',
    key: 'devicesEvent',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '包序号',
    dataIndex: 'packNumber',
    key: 'packNumber',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: 'CCID',
    dataIndex: 'ccid',
    key: 'ccid',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '控制指令',
    dataIndex: 'controlOlder',
    key: 'controlOlder',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '远程消音',
    dataIndex: 'remote_muffler',
    key: 'remote_muffler',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '远程自检',
    dataIndex: 'remote_selftest',
    key: 'remote_selftest',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '测试状态',
    dataIndex: 'result',
    key: 'result',
    render: (r) => {
      switch (r) {
        case 0:
          return <span style={styles.colorR}>未通过</span>
          // return <span style={styles.colorR}>  <Button >未通过</Button></span>
        case 1:
          return <span style={styles.colorG}>通过</span>
          // return <span style={styles.colorG}><Button type="primary">通过</Button></span>
        case 2:
          return '测试中'
        default:
          return <span style={styles.defaultV}>未知</span>
      }
    }
  },
  {
    title: '操作',
    key: 'opt',
  }
];

@onSetItemDecorator
class MyTable extends Component {
  constructor(props, context) {
    super(props, context);
  }

  onChange = (selectedRowKeys) => {
    this.props.select(selectedRowKeys);
  };
  renderOpt = (text, record) => {
    return (
      // text.result === 2 ? (
        <span>
              <Button type="primary" className="cursor" style={styles.confirmB}
              onClick={() => this.props.handlePass([record.imei], 1)}
              >通过</Button>
          <Button type="ghost" className="cursor"
           onClick={() => this.props.handlePass([record.imei], 0)}
          >不通过</Button>
            			</span>
      // ) : null
    );
  }

  handleColumns = (columns) => {
    return columns.map((c,i) => {
      return c.key !== 'opt' ? c : {...c, render: this.renderOpt};
    });
  }

  render() {
    const { list, selectKeys, pagination, productKey = 'fa1c1dc4df9b4a3ca0dac26308ac3836' } = this.props;
    const rowSelection = {
      onChange: this.onChange,
      selectedRowKeys: selectKeys
    };
    let currentCol = columns[productKey].map((item) => {
      return columnsItems.find(colItem => colItem.key === item);
    });
    currentCol = this.handleColumns(currentCol);
    return (
      <Table
        rowKey="imei"
        dataSource={list}
        columns={currentCol}
        pagination={pagination}
        // rowSelection={rowSelection}
        scroll={currentCol.length > 10 ? { x: 2000} : {}}
      />
    );
  }
}

MyTable.propTypes = {};

export default MyTable;
